"""
@Author  : 康帅
@Time    : 2021/6/29 17:27
@Function: 类装饰器
"""


from functools import wraps


class Login(object):
    """ 写入日志文件类装饰器 """
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + 'was called'
            print(log_string)

            with open(self.logfile, 'a') as opened_file:
                opened_file.write(log_string + '\n')

            self.notify()
            return func(*args, **kwargs)

        return wrapped_function

    def notify(self):
        pass


class EmailLogin(Login):
    """ 一个Login的实现版本，可以在函数调用时发送email给管理员 """
    # 通过继承类装饰器，不仅可以将日志写入文件而且添加发送邮件的功能
    def __init__(self, email='admin@163.com', *args, **kwargs):
        self.email = email
        super(EmailLogin, self).__init__(*args, **kwargs)

    def notify(self):
        """
        发送邮件内容
        :return:
        """
        pass


@EmailLogin()
def my_func1():
    pass


if __name__ == '__main__':
    my_func1()
